package ru.spb.osll.myfit.wikiverifier.verifier;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;

import ru.spb.osll.myfit.common.TableCell;
import ru.spb.osll.myfit.util.CollectionUtils;
import ru.spb.osll.myfit.util.StringUtils;
import util.GracefulNamer;

public abstract class BasicFixtureVerifier implements TableVerifier {
	protected String getClassName(Vector<TableCell> tableLine) {
		return GracefulNamer.disgrace(tableLine.get(0).getText());
	}
	
	protected String getMethodName(Vector<TableCell> tableLine) {
		List<TableCell> even = CollectionUtils.filterEven(tableLine);
		return GracefulNamer.disgrace(StringUtils.join(even, " "));
	}
	
	protected int parameterCount(Vector<TableCell> line) {
		return CollectionUtils.filterOdd(line).size();
	}

	protected Method findMethod(String methodName, Class<?> fixtureClass) {
		for (Method method : fixtureClass.getMethods()) {
			if (method.getName().equalsIgnoreCase(methodName)) {
				return method;
			}
		}
		
		return null;
	}
	
	protected boolean verifyMethodLine(Class<?> fixtureClass, Vector<TableCell> line) {
		String methodName = getMethodName(line);
		Method method = findMethod(methodName, fixtureClass);

		if (method == null) {
			return false;
		}

		if (method.getParameterTypes().length != parameterCount(line)) {
			return false;
		}
		
		return true;
	}

}
